(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.eventDoccfgViewController', eventDoccfgViewController);
	eventDoccfgViewController.$inject = [ '$scope', '$timeout', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig'];
	function eventDoccfgViewController($scope, $timeout, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("eventDoccfgViewController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getEventDoccfgOptions : {
				url : 'do/cda/dataList/eventDoccfgOptions',
				method : 'post'
			},
			getEventDoccfgLayout : {
				url : 'do/cda/eventDoccfg/layout',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.validatorConfig = validatorConfig;
		ctrl.eventDoccfg = {};
		ctrl.filterEventCodes = ctrl.dialogsModule.params.filterEventCodes || [];
		ctrl.prodType = ctrl.dialogsModule.params.prodType;
		ctrl.formName = "eventDoccfgForm";
		
		ctrl.init = function() {
			initOptions().then(function() {
				var tmp = ctrl.dialogsModule.params.eventDoccfg || {};
				tmp.oldDeliveryType = undefined;
				initLayoutConfig(tmp).then(function() {
					$timeout(function(){
						ctrl.eventDoccfg = tmp || {};
						ctrl.eventDoccfgOld = ctrl.dialogsModule.params.eventDoccfgOld;
					});
				});
			});
		};
		
		function initOptions() {
			var defer = $q.defer();
			var params = {};
			params.prodType = ctrl.prodType;
			getEventDoccfgOptions(params).then(function(response) {
				ctrl.eventCodeOptions = response.eventCodeOptions;
				ctrl.allDocTypeOptions = response.docTypeOptions;
				ctrl.templateNameOptions = response.templateNameOptions;
				ctrl.deliveryTypeOptions = response.deliveryTypeOptions;
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.localeCodeOptions = response.localeCodeOptions;
				defer.resolve();
			});
			return defer.promise;
		}
		function getEventDoccfgOptions(params) {
			var defer = $q.defer();
			ajaxService.getEventDoccfgOptions(params).then(function(response) {
				var eventDoccfgOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						eventDoccfgOptions[key] = tempValue;
					});
				}
				defer.resolve(eventDoccfgOptions);
			});
			return defer.promise;
		}
		
		ctrl.eventCodeChanged = function(value) {
			if (ctrl.eventDoccfg.eventCode) {
				// filter docType by eventCode
				var docTypeOptionsData = [];
				for (var i = 0; i < ctrl.allDocTypeOptions.length; i++) {
					var _docType = ctrl.allDocTypeOptions[i];
					if (_docType.eventCode == ctrl.eventDoccfg.eventCode) {
						docTypeOptionsData.push(ctrl.allDocTypeOptions[i]);
					}
				}
				ctrl.docTypeOptions = docTypeOptionsData;
			}
		};
		
		ctrl.docTypeChanged = function(value) {
			if (ctrl.eventDoccfg.docType) {
			}
		};
		
		ctrl.localeCodeChanged = function(value) {
			if (ctrl.eventDoccfg.localeCode) {
			}
		};
		
		function initLayoutConfig (initEventDoccfg) {
			var defer3 = $q.defer();
			var params = {
				"action" : "eventDoccfgView"
			};
			ajaxService.getEventDoccfgLayout(params).then(function(response) {
				var resLayoutConfig = response.data;
				var gridConfigs = resLayoutConfig.formConfigs.general.gridConfigs;
				if (initEventDoccfg.deliveryType) {
					initEventDoccfg.oldDeliveryType = initEventDoccfg.deliveryType;
					if (gridConfigs) {
						angular.forEach(gridConfigs, function(gridConfig, key) {
							if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'DELIVERYINFO') {
								if (gridConfig.dynamicParameter.subCategory != "DELIINFO-" + initEventDoccfg.deliveryType) {
									gridConfig.active = false;
								}
							}
						});
					}
				} else {
					if (gridConfigs) {
						angular.forEach(gridConfigs, function(gridConfig, key) {
							if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'DELIVERYINFO') {
								gridConfig.active = false;
							}
						});
					}
				}
				ctrl.layoutConfig = resLayoutConfig;
				defer3.resolve();
			});
			
			return defer3.promise;
		};
		
		ctrl.deliveryTypeChanged = function(value) {
			if (ctrl.eventDoccfg.deliveryType && ctrl.eventDoccfg.deliveryType != ctrl.eventDoccfg.oldDeliveryType) {
				ctrl.eventDoccfg.oldDeliveryType = ctrl.eventDoccfg.deliveryType;
				var tmpModel = angular.copy(ctrl.eventDoccfg);
				var gridConfigs = ctrl.layoutConfig.formConfigs.general.gridConfigs;
				if (gridConfigs) {
					angular.forEach(gridConfigs, function(gridConfig, key) {
						if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'DELIVERYINFO') {
							if (gridConfig.dynamicParameter.subCategory == "DELIINFO-" + ctrl.eventDoccfg.deliveryType) {
								gridConfig.active = true;
							} else {
								gridConfig.active = false;
							}
						}
					});
					$timeout(function(){
						ctrl.eventDoccfg = tmpModel || {};
					})
				}
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
